using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace Riolinx.SocialConnekt.Community.WebParts.Features.Riolinx.SocialConnekt.Community.UserInfoListExtensions
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("4a556ea3-b4db-4da1-b3ec-077573096e6b")]
    public class RiolinxSocialConnektCommunityEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        private const string SOCIAL_PROVIDER_PROPERTY_INTERNALNAME = "SocialProvider";
        private const string PROFILE_COMPLETE_READY_PROPERTY_INTERNALNAME = "ProfileComplete";


        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            try
            {
                site.AllowUnsafeUpdates = true;
                site.CatchAccessDeniedException = false;

                SPList userInfoList = site.RootWeb.SiteUserInfoList;

                if (userInfoList.Fields.TryGetFieldByStaticName(SOCIAL_PROVIDER_PROPERTY_INTERNALNAME) == null)
                {
                    string strFieldName = userInfoList.Fields.Add(SOCIAL_PROVIDER_PROPERTY_INTERNALNAME, SPFieldType.Text, false);
                }

                SPField fieldSocialProvider = userInfoList.Fields.GetFieldByInternalName(SOCIAL_PROVIDER_PROPERTY_INTERNALNAME);
                fieldSocialProvider.Title = "Social Provider Name";
                fieldSocialProvider.ShowInEditForm = false;
                fieldSocialProvider.Update();

                if (userInfoList.Fields.TryGetFieldByStaticName(PROFILE_COMPLETE_READY_PROPERTY_INTERNALNAME) == null)
                {
                    string strFieldName = userInfoList.Fields.Add(PROFILE_COMPLETE_READY_PROPERTY_INTERNALNAME, SPFieldType.Boolean, false);
                }

                SPField fieldProfileComplete = userInfoList.Fields.GetFieldByInternalName(PROFILE_COMPLETE_READY_PROPERTY_INTERNALNAME);
                fieldProfileComplete.Title = "Profile Complete";
                fieldProfileComplete.ShowInEditForm = false;
                fieldProfileComplete.Update();

                SPView view = userInfoList.DefaultView;
                if (!view.ViewFields.Exists(SOCIAL_PROVIDER_PROPERTY_INTERNALNAME))
                {
                    view.ViewFields.Add(fieldSocialProvider);
                    view.Update();
                }
                if (!view.ViewFields.Exists(PROFILE_COMPLETE_READY_PROPERTY_INTERNALNAME))
                {
                    view.ViewFields.Add(fieldProfileComplete);
                    view.Update();
                }
            }
            catch (Exception ex)
            {

                throw new Exception("An error occurred: " + ex.ToString());
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
